package com.hfzy.ihk.web.websocket.job;

import com.alibaba.fastjson.JSONObject;
import com.hfzy.ihk.common.util.date.DateUtils;
import com.hfzy.ihk.common.support.job.Job;
import com.hfzy.ihk.web.websocket.constant.Constants;
import com.hfzy.ihk.web.websocket.support.SampleCache;
import com.hfzy.ihk.web.websocket.support.WebSocketManager;
import com.hfzy.ihk.web.websocket.utils.SpringWebSocketUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/7.
 *
 * @author wws
 */
@Component
public class AutoCheckAndCleanWebSocket implements Job , InitializingBean{

    @Resource
    TaskScheduler taskScheduler;
    @Override
//    @Scheduled(cron = "0 0 2 * * ?")
    public void execute() {

        SampleCache.countConnectTime.clear();
        SampleCache.sessionIdTokenMap.clear();

        //关闭浏览器所有连接
        Map<String,String> map = new HashMap<>();
        int sessionCount=0;
        for(WebSocketSession webSocketSession: WebSocketManager.browserTokenWebSocketSessionMap.values()){

            if(webSocketSession==null) {
                continue;
            }

            if(webSocketSession.isOpen()){
                map.put(Constants.WEB_SOCKET_JSON_KEY_MSG_TYPE, Constants.WEB_SOCKET_MSG_TYPE_NOT_RE_WEBSOCKET);
                map.put(Constants.WEB_SOCKET_JSON_KEY_CODE, Constants.FAIL);
                map.put(Constants.WEB_SOCKET_JSON_KEY_DESC, "job close");
                map.put(Constants.WEB_SOCKET_JSON_KEY_TIME, DateUtils.formatDate(new Date(), DateUtils.DATE_FORMAT_DATETIME_SSS));
                /**每个连接都通知，不重连*/
                SpringWebSocketUtils.pushToClient(webSocketSession, JSONObject.toJSONString(map));
                map.clear();
                try {
                    webSocketSession.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                sessionCount++;
            }
        }

        System.out.println("AutoCheckAndCleanWebSocket job,clean browser webSocket size:"+sessionCount);

        sessionCount=0;

        for(WebSocketSession webSocketSession:WebSocketManager.appUserIdOrMacWebSocketSessionMap.values()) {

            if(webSocketSession==null)
                continue;

            if (webSocketSession.isOpen()) {
                try {
                    webSocketSession.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println("AutoCheckAndCleanWebSocket job,clean app webSocket size:"+sessionCount);
    }



    @Override
    public void afterPropertiesSet() throws Exception {
        /**
         * 每天晚上两点回收
         */
        taskScheduler.schedule(()->{execute();},new CronTrigger("0 0 2 * * ?"));
    }
}
